package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.orderinfoDao;
import org.netEase.entity.order;
import org.netEase.entity.orderInfo;
import org.netEase.util.BaseDao;

public class orderinfoDaoImpl extends BaseDao implements orderinfoDao {

	@Override
	public int addOI(orderInfo orderInfo) {
		String sql = "insert into orderInfo(oNo,sid,sname,ocount,unit_price,sum_price,jtgg,Simg)values(?,?,?,?,?,?,?,?)";
		return super.executeUpdate(sql, orderInfo.getOno(),orderInfo.getSid(),orderInfo.getSname(),orderInfo.getOcunt(),orderInfo.getUnitprice(),orderInfo.getSumprice(),orderInfo.getStypegg(),orderInfo.getSimg());
	}

	@Override
	public List<orderInfo> getbyorderId(String orderId) {
		List<orderInfo> list = new ArrayList<>();
		String sql = "SELECT * FROM orderinfo oi INNER JOIN order_list ol ON oi.ono = ol.ono WHERE ol.olId = ?";
		ResultSet rs = super.executeQuery(sql, orderId);
		try {
			while (rs.next()) {
				orderInfo orderInfo = new orderInfo();
				orderInfo.setOcunt(rs.getInt("OCount"));
				orderInfo.setOno(rs.getInt("ono"));
				orderInfo.setOtime(rs.getTimestamp("o_time"));
				orderInfo.setSid(rs.getInt("sid"));
				orderInfo.setSname(rs.getString("sname"));
				orderInfo.setStypegg(rs.getString("jtgg"));
				//orderInfo.setSumprice(rs.getInt("OCount")*rs.getDouble("unit_price"));
				orderInfo.setSumprice(rs.getDouble("sum_price"));
				orderInfo.setUnitprice(rs.getDouble("unit_price"));
				orderInfo.setSimg(rs.getString("simg"));
				
				list.add(orderInfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

}
